package com.liangzhong.modules.goldpalace.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.GoldPalaceEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxyconstant.goldpalace.GoldPalaceConst;
	import com.lz.football.module.goldpalace.IGoldPalaceView;
	
	import flash.display.DisplayObject;
	
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *黄金12宫 
	 * @author lake
	 * 
	 */	
	public class GoldPalaceMediator extends ModuleMediator
	{
		public static const MediatorName:String= "GoldPalaceMediator";
		
		public function GoldPalaceMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get view():IGoldPalaceView
		{
			return viewComponent as IGoldPalaceView;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModule);
			(view as DisplayObject).addEventListener(GoldPalaceEvent.Active_Player,onActivePlayer);
			(view as DisplayObject).addEventListener(GoldPalaceEvent.Change_Award,onChangeAward);
			(view as DisplayObject).addEventListener(GoldPalaceEvent.Drop_Time,onDropTime);
			(view as DisplayObject).addEventListener(GoldPalaceEvent.Get_Award,onGetAward);
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModule);
			(view as DisplayObject).removeEventListener(GoldPalaceEvent.Active_Player,onActivePlayer);
			(view as DisplayObject).removeEventListener(GoldPalaceEvent.Change_Award,onChangeAward);
			(view as DisplayObject).removeEventListener(GoldPalaceEvent.Drop_Time,onDropTime);
			(view as DisplayObject).removeEventListener(GoldPalaceEvent.Get_Award,onGetAward);
		}
		/**
		 *删除 
		 * @param e
		 * 
		 */		
		protected function onRemoveModule(e :ModuleEvent):void
		{
			removeMediator();
		}
		/**
		 * 点亮球员
		 * @param e
		 * 
		 */		
		private function onActivePlayer(e :GoldPalaceEvent):void
		{
			this.sendNotification(ApplicationConstants.GOLDPALACE_ACTIVE_PLAYER,e.param);
		}
		/**
		 * 奖励更换
		 * @param e
		 * 
		 */		
		private function onChangeAward(e :GoldPalaceEvent):void
		{
			this.sendNotification(ApplicationConstants.GOLDPALACE_CHANGE_AWARD,e.param);
		}
		/**
		 * 点券去除CD时间
		 * @param e
		 * 
		 */		
		private function onDropTime(e :GoldPalaceEvent):void
		{
			this.sendNotification(ApplicationConstants.GOLDPALACE_DROP_TIME,e.param);
		}
		/**
		 * 领取奖励 
		 * @param e
		 */		
		private function onGetAward(e :GoldPalaceEvent):void
		{
			this.sendNotification(ApplicationConstants.GOLDPALACE_GET_AWARD,e.param);
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [GoldPalaceConst.ACTIVE_PLAYER_RES,
				GoldPalaceConst.CHANGE_AWARD_RES,
				GoldPalaceConst.DROP_TIME_RES,
				GoldPalaceConst.GET_AWARD_RES,
				GoldPalaceConst.GET_INFO_RES];
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case GoldPalaceConst.ACTIVE_PLAYER_RES://激活球员 
				{
					view.activePlayer(notification.getBody());
					break;
				}
				case GoldPalaceConst.CHANGE_AWARD_RES://更换奖励
				{
					view.changeAward(notification.getBody());
					break;
				}
				case GoldPalaceConst.DROP_TIME_RES://去除冷却时间
				{
					view.dropTime(notification.getBody());
					break;
				}
				case GoldPalaceConst.GET_AWARD_RES://领取奖励
				{
					view.getAward(notification.getBody());
					break;
				}
				case GoldPalaceConst.GET_INFO_RES://获取信息
				{
					view.getInfo(notification.getBody());
					break;
				}
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			
			if(ResourceManager.instance.isContainResource(ResourceConst.GOLDPALACE_SWF))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var config :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).goldpalace[0];
				bytesTotal = int(config.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in config.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			super.onLoadComplete(e);
			showUI();
			ResourceManager.instance.putResource(ResourceConst.GOLDPALACE_SWF,resList);
		}
		/**
		 *显示界面 
		 */		
		private function showUI():void
		{
			if(AppData.getInstance().goldPalaceManager.goldPalaceVo.awardPos <= 0)
			{
				this.sendNotification(ApplicationConstants.GOLDPALACE_GET_INFO);
			}
			var cView :IGoldPalaceView = Reflection.createInstance("com.lz.football.module.goldpalace.GoldPalaceView");
			cView.initUI();
			viewComponent = cView;
			PopUpManager.addPopUp(view as DisplayObject,GlobalDef.GameWindowContainer,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageWidth,false);
			addEventListeners();
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			view.destroyView();
			viewComponent = null;
		}
	}
}